﻿using MES.Server.Entities;
using MES.Server.IService;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MES.Server.Service
{
    public class RoleService : BaseService, IRoleService
    {
        public RoleService(ISqlSugarClient client) : base(client)
        {
        }
        public bool CheckRoleName(string roleName, int id)
        {
            return _Client.Queryable<RoleEntity>()
                .Any(r => r.RoleName == roleName &&
                        r.RoleId != id);
        }

        public int Delete(int[] ids)
        {
            // 删除RoleMenu、RoleUser
            int count = 0;
            try
            {
                _Client.Ado.BeginTran();
                _Client.Deleteable<RoleMenuEntity>().Where(rm => ids.Contains(rm.RoleId)).ExecuteCommand();
                _Client.Deleteable<RoleUserEntity>().Where(ru => ids.Contains(ru.RoleId)).ExecuteCommand();
                count = _Client.Deleteable<RoleEntity>().In(ids).ExecuteCommand();
                _Client.Ado.CommitTran();
            }
            catch (Exception ex)
            {
                _Client.Ado.RollbackTran();
                throw ex;
            }
            return count;
        }

        public int DeleteRoleUser(int rid, int uid)
        {
            return _Client.Deleteable<RoleUserEntity>()
                 .Where(ru => ru.RoleId == rid && ru.UserId == uid)
                 .ExecuteCommand();
        }

        public RoleEntity[] GetAllRoles(string key)
        {
            return _Client.Queryable<RoleEntity>()
                .Includes(r => r.Menus.MappingField(m => m.RoleId, () => r.RoleId).ToList())
                .Includes(r => r.Users.MappingField(u => u.RoleId, () => r.RoleId).ToList())
                .Where(r =>
                    string.IsNullOrEmpty(key) ||
                    r.RoleName.Contains(key) ||
                    r.RoleDesc.Contains(key)).ToArray();
        }

        public RoleEntity[] GetRoleByIds(int[] id)
        {
            return _Client.Queryable<RoleEntity>()
                .Where(r => id.Contains(r.RoleId))
                .ToArray();
        }

        public int Update(RoleEntity role)
        {
            int count = 0;
            if (role.RoleId == 0)
            {
                count = _Client.Insertable(role).IgnoreColumns(r => r.RoleId).ExecuteCommand();
            }
            else
            {
                count = _Client.Updateable(role).ExecuteCommand();
            }

            return count;
        }

        public int UpdateRoleMenus(RoleMenuEntity[] rms)
        {
            _Client.Deleteable<RoleMenuEntity>()
                .Where(rm => rm.RoleId == rms[0].RoleId)
                .ExecuteCommand();

            return _Client.Insertable(rms).ExecuteCommand();
        }

        public int UpdateRoleUsers(RoleUserEntity[] users)
        {
            _Client.Deleteable<RoleUserEntity>()
                .Where(rm => rm.RoleId == users[0].RoleId)
                .ExecuteCommand();

            return _Client.Insertable(users).ExecuteCommand();
        }
    }
}
